const service = require('../../services/clock-in-template-service');
const messages = require('../../common/messages');
const RefreshTrigger = require('../../common/refresh-trigger');

Page({
  data: {
    templateList: [
      // {
      //   id: 1,
      //   iconName: 'accessibility',
      //   name: '帮他行',
      // },
      // {
      //   id: 2,
      //   iconName: 'anchor',
      //   name: '懂孩子',
      // },
      // {
      //   id: 3,
      //   iconName: 'battery-charging',
      //   name: '力量',
      // },
    ],
  },
  refreshTrigger: null,

  onLoad() {
    this.refreshTrigger = new RefreshTrigger(this.doRefreshItem);
  },

  onReady() {},

  onShow() {
    this.refreshTrigger.refresh();
  },

  onHide() {},

  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},

  doRefreshItem() {
    // service
    //   .queryAll()
    //   .then((res) =>
    //     this.setData({
    //       templateList: res.data,
    //     }),
    //   )
    //   .catch(messages.showError);
  },
  doCreateItem() {
    // this.refreshTrigger.navigateTo({ url: '/pages/template-item/index' });
  },
  doModifyItem(e) {
    const { id } = e.currentTarget.dataset;
    this.refreshTrigger.navigateTo({ url: `/pages/template-item/index?id=${id}` });
  },
  doDeleteItem(e) {
    const { item } = e.currentTarget.dataset;
    messages
      .showModal(`确定要删除此项目(${item.name})吗？`)
      .then(() => service.delete(item.id))
      .then(() => this.doRefreshItem())
      .catch(messages.showError);
  },
});
